package edu.mju.eduorder.service.impl;

import edu.mju.commonutils.orderVo.CourseWebVoOrder;
import edu.mju.commonutils.orderVo.UserMemberOrder;
import edu.mju.eduorder.client.EduClient;
import edu.mju.eduorder.client.UserClient;
import edu.mju.eduorder.entity.StudyOrder;
import edu.mju.eduorder.mapper.StudyOrderMapper;
import edu.mju.eduorder.service.StudyOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-10-05
 */
@Service
public class StudyOrderServiceImpl extends ServiceImpl<StudyOrderMapper, StudyOrder> implements StudyOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UserClient userClient;

    @Override
    public String createOrder(String courseId, String userId) {
        //远程调用：根据用户id获取用户信息
        UserMemberOrder userInfoOrder = userClient.getUserInfoOrder(userId);
        //远程调用：根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建Order对象，向Order对象设置数据
        StudyOrder order = new StudyOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(userId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);
        order.setPayType(1);

        //数据加入数据库
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
